package com.hcrazy.manchester.app.services;

import java.lang.reflect.Type;
import java.util.List;

import org.joda.time.DateTime;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hcrazy.manchester.app.jdbc.AdvertDAO;
import com.hcrazy.manchester.app.jdbc.SecondaryDAO;
import com.hcrazy.manchester.app.pojo.Advert;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Secondary;
import com.hcrazy.manchester.app.utils.Constant;

public class SecondaryService {

	private SecondaryDAO secondaryDAO;
	
	private AdvertDAO advertDAO;
	
	public AdvertDAO getAdvertDAO() {
		return advertDAO;
	}

	public void setAdvertDAO(AdvertDAO advertDAO) {
		this.advertDAO = advertDAO;
	}

	public SecondaryDAO getSecondaryDAO() {
		return secondaryDAO;
	}

	public void setSecondaryDAO(SecondaryDAO secondaryDAO) {
		this.secondaryDAO = secondaryDAO;
	}

	public ErrorMsg addOrUpdateSecondary(Secondary secondary) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		long secondaryId = 0;
		//设置发布时间
		DateTime date = new DateTime();
		
		//如果有效时间为空，则添加默认，默认为30天
		if(null == secondary.getPublishValidateDate() || "".equals(secondary.getPublishValidateDate())){
			DateTime validateDt = date.plusDays(30);
			secondary.setPublishValidateDate(validateDt.toString("yyyy-MM-dd"));
		}
		
		if(secondary.getInfoId()>0){
			secondary.setPraiseCount(secondary.getPraiseCount() + secondary.getPraiseBaseCount());
			secondary.setCommentCount(secondary.getCommentCount() + secondary.getCommentBaseCount());
			secondary.setSelledCount(secondary.getSelledCount() + secondary.getSelledBaseCount());
			
			secondary.setUpdateDate(date.toString("yyyy-MM-dd hh:mm:ss"));
			secondaryDAO.updateSecondary(secondary);
			secondaryId = secondary.getInfoId();
		}else{
			if(null != secondary && secondary.getPraiseBaseCount() > 0){
				secondary.setPraiseCount(secondary.getPraiseBaseCount());
			}
			if(null != secondary && secondary.getCommentBaseCount() > 0){
				secondary.setCommentCount(secondary.getCommentBaseCount());
			}
			if(null != secondary && secondary.getSelledBaseCount() > 0){
				secondary.setSelledBaseCount(secondary.getSelledBaseCount());
			}
			secondary.setPublishDate(date.toString("yyyy-MM-dd hh:mm:ss"));
			secondaryId = secondaryDAO.addSecondary(secondary);
		}
		
		if(secondary.getAdvertStr() != null && !secondary.getAdvertStr().equals("")){
			Gson gson = new Gson();
			Type type = new TypeToken<Advert>(){}.getType();
			Advert ad = gson.fromJson(secondary.getAdvertStr(),type);
			if(null != ad && ad.getAdContent() != null && !ad.getAdContent().equals("")){
				advertDAO.deleteAdvert(secondaryId,Constant.COMM_SECONDARY_INFO_MODULE);
				
				ad.setModuleId(secondaryId);
				ad.setModuleType(Constant.COMM_SECONDARY_INFO_MODULE);
				advertDAO.addAdvert(ad);
			}
			
		}
		
		return msg;
	}
	
	public PageModel getSecondaryInfos(PageModel model,String search,int sortId) throws Exception{
		return secondaryDAO.getPagedInfos(model, search, sortId);
	}
	
	public PageModel getSecondaries(PageModel model,int sortId,int status,int priceRange,int priceStart,
			int priceEnd,String keywords,long userId) throws Exception{
		return secondaryDAO.getPagedInfos(model, sortId, status, priceRange, priceStart, priceEnd, keywords,userId);
	}
	
	public Secondary getInfoById(long infoId) throws Exception{
		Secondary seconday = secondaryDAO.getById(infoId);
		Advert advert = advertDAO.getAdvert(infoId,Constant.COMM_SECONDARY_INFO_MODULE);
		if(null != advert)
			seconday.setAdvert(advert);
		return seconday;
	}
	
	public void delSecondary(long infoId) throws Exception{
		secondaryDAO.delSecondary(infoId);
	}
	
	public void delSecondary(long infoId,long userId) throws Exception{
		Secondary secondary = secondaryDAO.getById(infoId);
		if(null != secondary && secondary.getUserId() > 0 && secondary.getUserId() == userId)
			secondaryDAO.delSecondary(infoId);
	}
	
	public void setViews(long id) throws Exception{
		//获取对象
		Secondary secondary = secondaryDAO.getById(id);
		long views = secondary.getViews()+1;
		//计数+1
		secondaryDAO.setViews(views,id);
	}
	
	public List<Secondary> getSecondaries(int size) throws Exception{
		return secondaryDAO.getSecondaries(size);
	}
}
